/**
 * 
 */
package com.mobile2you.nci.android.data;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.mobile2you.nci.android.domain.Annuaires;
import com.mobile2you.nci.android.domain.Centres;
import com.mobile2you.nci.android.domain.News;
import com.mobile2you.nci.android.domain.Services;

/**
 * @author JBromo
 * 
 */
public class SqlHelper extends SQLiteOpenHelper {

	private static SqlHelper helper;
	public static final String KEY_CENTRE = "centres";
	public static final String KEY_SERVICE = "services";
	public static final String KEY_NEWS = "news";
	public static final String KEY_ANNUAIRES = "annuaire";

	SQLiteDatabase db;

	public static SqlHelper getInstance(Context context) {
		if (helper == null) {
			helper = new SqlHelper(context);
		}
		return helper;
	}

	/**
	 * @param context
	 * @param name
	 * @param factory
	 * @param version
	 */
	private SqlHelper(Context context) {
		super(context, "ncidata.db", null, 1);
		db = getWritableDatabase();
		createTable();

	}

	public void addUpdate(String datakey, String update, int value) {
		ContentValues values = new ContentValues();
		System.out.println(" add update :" + update + " value :" + value);
		if (update != null) {
			db.delete("updatedata", "datakey= ?", new String[] { datakey });
			values.put("datakey", datakey);
			values.put("update_at", update);
			values.put("toupdate", value);
			db.insert("updatedata", null, values);
		}

	}

	public boolean alreadyLogin() {
		Cursor c = db.query("logindata", null, null, null, null, null, null);
		try {
			if (c != null && c.moveToNext()) {
				return true;

			}
		} catch (Exception e) {
		}

		return false;
	}

	private void createTable() {
		db.execSQL("CREATE TABLE IF NOT EXISTS updatedata(datakey  TEXT UNIQUE, update_at  TEXT, toupdate  INTEGER)");
		db.execSQL("CREATE TABLE IF NOT EXISTS centredata(data TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS newsdata(data TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS annuaire(data TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS servicedata(data TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS logindata(login TEXT, password TEXT)");
	}

	public String getLastUpdate(String datakeys) {
		String requete = "select update_at from  updatedata where datakey ="
				+ "'" + datakeys + "'";
		String result = "-1";
		Cursor cursor = db.rawQuery(requete, null);
		if (cursor != null && cursor.moveToNext()) {
			result = cursor.getString(0);
		}

		return result;
	}

	public List<Annuaires> getLocalAnnuaire() {
		Cursor c = db.query("annuaire", null, null, null, null, null, null);
		List<Annuaires> list = new ArrayList<Annuaires>();
		try {
			if (c != null && c.moveToNext()) {
				System.out.println(" Enter enter...ANNUAUIRE");
				String value = c.getString(0);
				JSONArray jsonArray = null;
				jsonArray = new JSONArray(value);
				list = DataParseur.parseAnnuaire(jsonArray);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return list;
	}

	public List<Centres> getLocalCentres() {
		Cursor c = db.query("centredata", null, null, null, null, null, null);
		try {
			if (c != null && c.moveToNext()) {
				String value = c.getString(0);
				JSONArray jsonArray;
				jsonArray = new JSONArray(value);
				return DataParseur.parseCentres(jsonArray);

			}
		} catch (Exception e) {
		}
		return new ArrayList<Centres>();
	}

	public String[] getLocalLogin() {
		Cursor c = db.query("logindata", new String[] { "login", "password" },
				null, null, null, null, null);
		String[] result = new String[2];

		if (c != null && c.moveToNext()) {
			System.out.println("Cursor");
			result[0] = c.getString(0);
			result[1] = c.getString(1);
		}

		if (c != null) {
			c.close();
		}
		System.out.println(result);
		return result;
	}

	public List<News> getLocalNews() {
		Cursor c = db.query("newsdata", null, null, null, null, null, null);
		try {
			if (c != null && c.moveToNext()) {
				String value = c.getString(0);
				JSONArray jsonArray;
				jsonArray = new JSONArray(value);
				return DataParseur.parseNews(jsonArray);

			}
		} catch (Exception e) {
		}
		return new ArrayList<News>();
	}

	public List<Services> getLocalServices() {
		Cursor c = db.query("servicedata", null, null, null, null, null, null);
		try {
			if (c != null && c.moveToNext()) {
				String value = c.getString(0);
				JSONArray jsonArray;
				jsonArray = new JSONArray(value);
				return DataParseur.parseServices(jsonArray);

			}
		} catch (Exception e) {
		}
		return new ArrayList<Services>();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS updatedata(datakey  TEXT UNIQUE, update_at  TEXT, toupdate  INTEGER)");
		db.execSQL("CREATE TABLE IF NOT EXISTS centredata(data TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS newsdata(data TEXT)");
		db.execSQL("CREATE TABLE IF NOT EXISTS servicedata(data TEXT)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void saveAnnuaire(JSONArray jsonArray) {
		ContentValues values = new ContentValues();
		db.delete("annuaire", null, null);
		values.put("data", jsonArray.toString());
		db.insert("annuaire", null, values);
	}

	public void saveCentres(JSONArray jsonArray) {
		ContentValues values = new ContentValues();
		db.delete("centredata", null, null);
		values.put("data", jsonArray.toString());
		db.insert("centredata", null, values);
	}

	public void saveLogin(String login, String password) {
		ContentValues values = new ContentValues();
		values.put("login", login);
		values.put("password", password);
		long i = db.insert("logindata", null, values);
		System.out.println("Login :" + i);
		System.out.println("Login :" + password);
	}

	public void saveNews(JSONArray jsonArray) {
		ContentValues values = new ContentValues();
		db.delete("newsdata", null, null);
		values.put("data", jsonArray.toString());
		db.insert("newsdata", null, values);
	}

	public void saveServices(JSONArray jsonArray) {
		ContentValues values = new ContentValues();
		db.delete("servicedata", null, null);
		values.put("data", jsonArray.toString());
		db.insert("servicedata", null, values);
	}

	public boolean toUpdate(String datakeys) {

		String requete = "select toupdate from  updatedata where datakey ="
				+ "'" + datakeys + "'";
		int result = 1;
		Cursor cursor = db.rawQuery(requete, null);
		if (cursor != null && cursor.moveToNext()) {
			result = cursor.getInt(0);
		}
		System.out.println(" DataKey " + datakeys + " UPDATE ? "
				+ (result != 0));
		return result != 0;
	}

}
